﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;

namespace Business
{
    public partial class User
    {
        private string _UnsignFullName = String.Empty;
        public string UnsignFullName
        {
            get
            {
                return Common.ConvertUtils.ConvertToUnSign(_DisplayName).ToLower();
            }
        }

        private List<Business.Role> _ListRole;
        public List<Business.Role> ListRole
        {
            get { return _ListRole; }
            set { _ListRole = value; }
        }

        private List<Business.UserRole> _ListUserRole;
        public List<Business.UserRole> ListUserRole
        {
            get
            {
                _ListUserRole = new List<UserRole>();
                foreach (Business.Role item in _ListRole)
                {
                    Business.UserRole userRole = new Business.UserRole();
                    userRole.UserID = this.ID;
                    userRole.RoleID = item.RoleID;
                    _ListUserRole.Add(userRole);
                }
                return _ListUserRole;
            }
        }
    }

    public partial class User
    {
        public Common.LoginInfo LoginWeb(ref string MessageSystemError, Common.LoginInfo LoginInfo)
        {
            DataClassesDataContext context = null;
            try
            {
                LoginInfo.UserName = LoginInfo.UserName.ToLower().Trim();
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                //Lấy user theo username
                var query = from user in context.Users
                            where user.UserName == LoginInfo.UserName
                            && user.Status == (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE
                            select user;
                var firstUser = query.FirstOrDefault();
                if (firstUser == null)
                {
                    LoginInfo.LoginResult = Common.ERROR_CODE.ERROR_CODE_LOGIN.WRONG_USERNAME;
                    return LoginInfo;
                }
                string encodePassword = Common.EncryptionUtil.Encode(LoginInfo.Password, LoginInfo.UserName.Trim().ToLower());
                //Kiểm tra mật khẩu
                if (firstUser.Password != encodePassword)
                    LoginInfo.LoginResult = Common.ERROR_CODE.ERROR_CODE_LOGIN.WRONG_PASS;
                else
                {
                    LoginInfo.LoginResult = Common.ERROR_CODE.ERROR_CODE_LOGIN.SUCCESS;
                    Common.UserInfo userInfo = new Common.UserInfo();
                    userInfo.UserID = firstUser.ID;
                    userInfo.Name = firstUser.DisplayName;
                    userInfo.UserName = firstUser.UserName;
                    userInfo.UserCode = firstUser.UserCode;
                    LoginInfo.UserInfo = userInfo;
                }
                context.SubmitChanges();
                return LoginInfo;
            }
            catch (Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                LoginInfo.LoginResult = Common.ERROR_CODE.ERROR_CODE_LOGIN.SYSTEM_ERROR;
                return LoginInfo;
            }
            finally
            {
                context = null;
            }
        }
     
        public Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD ChangePassword(ref string MessageSystemError, string UserName, string OldPassword, string NewPassword)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from user in context.Users
                            where user.UserName == UserName
                            && user.Status == (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE
                            select user;

                //Lấy user đầu tiên
                User firstUser = query.FirstOrDefault();
                if (firstUser == null)
                {
                    return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.WRONG_USERNAME;
                }

                //Kiểm tra mật khẩu cũ
                string tempPass = Common.EncryptionUtil.Encode(OldPassword, UserName);
                if (tempPass == firstUser.Password)
                {
                    User obj = context.Users.Single(p => p.UserName == firstUser.UserName);
                    obj.Password = Common.EncryptionUtil.Encode(NewPassword, UserName);
                    context.SubmitChanges();
                    return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.SUCCESS;
                }
                else
                {
                    return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.WRONG_PASS;
                }
            }
            catch (Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }
        public Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD ChangePasswordMobile(ref string MessageSystemError, Common.UserInfo UserInfo, string OldPassword, string NewPassword)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from user in context.Users
                            where user.UserName == UserInfo.UserName
                            && user.Status == (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE
                            select user;

                //Lấy user đầu tiên
                User firstUser = query.FirstOrDefault();
                if (firstUser == null)
                {
                    return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.WRONG_USERNAME;
                }

                //Kiểm tra mật khẩu cũ
                string tempPass = Common.EncryptionUtil.Encode(OldPassword, UserInfo.UserName);
                if (tempPass == firstUser.Password)
                {
                    User obj = context.Users.Single(p => p.UserName == firstUser.UserName);
                    obj.Password = Common.EncryptionUtil.Encode(NewPassword, UserInfo.UserName);
                    context.SubmitChanges();
                    return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.SUCCESS;
                }
                else
                {
                    return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.WRONG_PASS;
                }
            }
            catch (Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_CHANGE_PASS_WORD.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_ITEMS_GET_BY_ID GetByID(ref string MessageSystemError, long ID, ref User User)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from item in context.Users
                            where item.ID == ID
                            select item;
                User = query.FirstOrDefault();
                if (User != null)
                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_GET_BY_ID.SUCCESS;
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_GET_BY_ID.DO_NOT_EXIST;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_GET_BY_ID.SYSTEM_ERROR;
            }
            finally
            { 
            
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE GetUserMenu(ref string MessageSystemError, Common.UserInfo UserInfo, ref List<Menu> ListMenu)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                //Lấy danh sách vai trò User
                var query1 = from uo in context.UserRoles
                             where uo.UserID == UserInfo.UserID
                             select uo;

                //Lấy danh sách menu theo vai trò
                var query2 = from item in query1
                             join roleMenu in context.RoleMenus on item.RoleID equals roleMenu.RoleID
                             join menu in context.Menus on roleMenu.MenuID equals menu.ID
                             select menu;

                List<Menu> listMenuTemp = new List<Menu>();
                listMenuTemp = query2.ToList();
                listMenuTemp = listMenuTemp.Distinct().ToList();

                //Tạo RootMenu
                var query3 = from rootMenu in context.Menus
                             join temp in query2 on rootMenu.ID equals temp.ID
                             where rootMenu.ParentID == 0
                             select rootMenu;
                ListMenu = query3.ToList();

                //Lấy danh sách SubMenu
                Business.Menu obj = new Business.Menu();
                obj.GetSubMenu(ref MessageSystemError, ref ListMenu, listMenuTemp);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE GetUserMenu(ref string MessageSystemError, Common.UserInfo UserInfo, ref List<Menu> ListMenu, string MenuType)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                //Lấy danh sách vai trò User
                var query1 = from uo in context.UserRoles
                             where uo.UserID == UserInfo.UserID
                             select uo;

                //Lấy danh sách menu theo vai trò
                var query2 = from item in query1
                             join roleMenu in context.RoleMenus on item.RoleID equals roleMenu.RoleID
                             join menu in context.Menus on roleMenu.MenuID equals menu.ID
                             where  menu.MenuType == MenuType && menu.Status != (int)Common.STATUS_CODE.ROW_STATUS.DEACTIVE
                             select menu;

                List<Menu> listMenuTemp = new List<Menu>();
                listMenuTemp = query2.ToList();
                listMenuTemp = listMenuTemp.Distinct().ToList();

                //Tạo RootMenu
                var query3 = from rootMenu in context.Menus
                             join temp in query2 on rootMenu.ID equals temp.ID
                             where rootMenu.ParentID == 0 && rootMenu.Status != (int)Common.STATUS_CODE.ROW_STATUS.DEACTIVE
                             select rootMenu;
                ListMenu = query3.ToList();

                //Lấy danh sách SubMenu
                Business.Menu obj = new Business.Menu();
                obj.GetSubMenu(ref MessageSystemError, ref ListMenu, listMenuTemp);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch (Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE GetAllUserSurveyForCombo(ref string MessageSystemError, ref List<Common.ComboItem> ListItem)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from item in context.Users
                            join userRole in context.UserRoles on item.ID equals userRole.UserID
                            join role in context.Roles on userRole.RoleID equals role.RoleID
                            where role.RoleName == Common.STATUS_CODE.ROLE_CODE.PG
                            where item.Status != (int)Common.STATUS_CODE.ROW_STATUS.DEACTIVE
                            select new Common.ComboItem
                            {
                                ID  = item.ID,
                                Text = item.DisplayName
                            };
                ListItem = query.ToList();
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE GetUserRole(ref string MessageSystemError, long UserID, ref List<Business.SimpleRole> ListSimpleRole)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from item in context.UserRoles
                            join role in context.Roles on item.RoleID equals role.RoleID
                            where item.UserID == UserID
                            select new SimpleRole
                            {
                                ID = role.RoleID,
                                Code = role.RoleName,
                                Name = role.RoleName,
                                Description = role.Remark
                            };
                ListSimpleRole = query.ToList();
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE HasRole(ref string MessageSytemError, long UserID, string RoleCode, ref bool HasRole)
        {
            try
            {
                List<SimpleRole> listSimpleRole = new List<SimpleRole>();
                Common.ERROR_CODE.ERROR_CODE_SIMPLE errCode = GetUserRole(ref MessageSytemError, UserID, ref listSimpleRole);
                if (errCode == Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS)
                {
                    int index = listSimpleRole.FindIndex(p => p.Code == RoleCode);
                    HasRole = (index != -1);
                    return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
                }
                return errCode;
            }
            catch (Exception ex)
            {
                MessageSytemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE GetUserHasRole(ref string MessageSystemError, string RoleName, ref List<Business.User> ListUser)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from userRole in context.UserRoles
                            join role in context.Roles on userRole.RoleID equals role.RoleID
                            join user in context.Users on userRole.UserID equals user.ID
                            where role.RoleName == RoleName
                            select user;
                ListUser = query.ToList();
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Business.Menu GetRolePrivilegeOnMenu(int UserID, string MenuCode)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                Menu menu = context.Menus.SingleOrDefault(p=>p.Code == MenuCode);
                var query = from userRole in context.UserRoles
                            join roleMenu in context.RoleMenus on userRole.RoleID equals roleMenu.RoleID
                            where userRole.UserID == UserID
                            && roleMenu.MenuID == menu.ID
                            select roleMenu;
                foreach (var item in query)
                {
                    menu.ViewApprovedItem = (Convert.ToBoolean(menu.ViewApprovedItem) || Convert.ToBoolean(item.ViewApprovedItem));
                }
                return menu;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                context = null;
            }
        }

        //Kiểm tra tài khoản đăng nhập có tồn tại
        private bool IsUserNameExist(string UserName, DataClassesDataContext context)
        {
            try
            {
                if (UserName.ToLower() == Common.STATUS_CODE.ROLE_CODE.ROOT.ToLower())
                    return true;
                if (context == null)
                    context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from item in context.Users
                            where item.UserName.ToLower() == UserName.ToLower()
                            select new { item.ID };
                return (query.Count() > 0);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT SearchUser(ref string MessageSystemError, UserSE se, ref List<Business.User> ListUser)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from item in context.Users
                            where item.Status != (int)Common.STATUS_CODE.ROW_STATUS.DEACTIVE
                            && (item.UserName == null || item.UserName.Contains(se.UserName))
                            select item;
                ListUser = query.ToList();

                ListUser = ListUser.FindAll(p => p.UnsignFullName == String.Empty || p.UnsignFullName.Contains(se.UnsignFullName));
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddUser(ref string MessageSystemError, Common.UserInfo UserInfo)
        {
            DataClassesDataContext context = null;
            try
            {
                if (UserInfo != null)
                {
                    context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                    if (!IsUserNameExist(this.UserName, context))
                    {
                        int count = context.Users.Count();
                        string userCode = (count + 1).ToString("0000");
                        this.Password = Common.EncryptionUtil.Encode(this.Password, this.UserName.ToLower());
                        this.Status = 1;
                        this.CreatedBy = UserInfo.UserID;
                        this.CreatedDateTime = DateTime.Now;
                        this.RowVersion = 1;
                        context.Users.InsertOnSubmit(this);
                        context.SubmitChanges();

                        //Thêm UserRole
                        context.UserRoles.InsertAllOnSubmit(this.ListUserRole);
                        context.SubmitChanges();
                        return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
                    }
                    else
                    {
                        MessageSystemError = Common.ERROR_CODE.ERROR_STRING.USERNAME_IS_EXIST;
                        return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.DUPPLICATE;
                    }
                }
                else
                    throw new Exception(Common.ERROR_CODE.ERROR_STRING.LOST_USER_SESSION);
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE UpdateUser(ref string MessageSystemError, Common.UserInfo UserInfo)
        {
            DataClassesDataContext context = null;
            try
            {
                if (UserInfo != null)
                {
                    context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                    User user = context.Users.Single(p => p.ID == this.ID);
                    user.DisplayName = this.DisplayName;
                    user.LastUpdatedBy = UserInfo.UserID;
                    user.LastUpdatedDateTime = DateTime.Now;
                    user.RowVersion = user.RowVersion + 1;
                    context.SubmitChanges();

                    //Cập nhật role
                    UserRole userRole = new Business.UserRole();
                    Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE err = userRole.UpdateUserRole(ref MessageSystemError, this.ID, this.ListRole);
                    if (err != Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS)
                        return err;
                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS;
                }
                else
                    throw new Exception(Common.ERROR_CODE.ERROR_STRING.LOST_USER_SESSION);
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE ResetPassword(ref string MessageSystemError, Common.UserInfo UserInfo, int UserID, string NewPassword)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                User user = context.Users.Single(p=>p.ID == UserID);
                string Password = Common.EncryptionUtil.Encode(NewPassword, user.UserName.ToLower());
                user.Password = Password;
                user.LastUpdatedBy = UserInfo.UserID;
                //user.LastUpdatedDatetime = DateTime.Now;
                user.RowVersion = user.RowVersion + 1;
                context.SubmitChanges();
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE ResetPassword(ref string MessageSystemError, int UserID, string NewPassword)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                User user = context.Users.Single(p => p.ID == UserID);
                string Password = Common.EncryptionUtil.Encode(NewPassword, user.UserName.ToLower());
                user.Password = Password;
                context.SubmitChanges();
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch (Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE Deactive(ref string MessageSystemError, long UserID)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                User user = context.Users.Single(p => p.ID == UserID);
                user.Status = (int)Common.STATUS_CODE.ROW_STATUS.DEACTIVE;
                context.SubmitChanges();
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }


        public static User getrole(string UserName)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from ou in context.Users
                            where ou.UserName == UserName || UserName == ""
                            select ou;
                return query.FirstOrDefault();
            }
            catch
            {
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }

        }
    }

    public partial class User
    { 
        //public static Team GetTeam(int UserID, DataClassesDataContext context)
        //{
        //    if (context == null)
        //        context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
        //    User user = context.Users.Single(p => p.ID == UserID);
        //    return user.Team;
        //}
    }

    public class UserSE
    {
        #region Tên tham số tìm kiếm
        public const string UserNameParam = "_UserName";
        public const string FullNameParam = "_FullName";
        public const string PhoneParam = "_Phone";
        #endregion

        private string _FullName = String.Empty;
        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value.Trim().ToLower(); }
        }

        private string _UnsignFullName = String.Empty;
        public string UnsignFullName
        {
            get { return Common.ConvertUtils.ConvertToUnSign(_FullName); }
        }

        private string _UserName = String.Empty;
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value.Trim().ToLower(); }
        }

        private string _Phone = String.Empty;
        public string Phone
        {
            get { return _Phone; }
            set { _Phone = value.Trim(); }
        }
    }
}
